package com.zzyl.controller.customer;


import cn.hutool.core.date.LocalDateTimeUtil;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.dto.ReservationDto;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationVo;
import com.zzyl.vo.TimeCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


@RestController
@RequestMapping("/customer/reservation")
@Api(tags = "参观预约接口")
public class CustomerReservationController extends BaseController {
    @Autowired
    CustomerReservationService customerReservationService;


    /**
     * 1 查询取消预约数量
     */
    @GetMapping("/cancelled-count")
    @ApiOperation(value = "查询取消预约数量")
    public ResponseResult<Integer> getCancelledReservationCount() {
        Long userId = UserThreadLocal.getUserId();
        Integer count = customerReservationService.countCancelledReservation(userId);
        return success(count);

    }


    /**
     * 2 查询每个时间段剩余预约次数
     */
    @GetMapping("/countByTime")
    @ApiOperation(value = "查询每个时间段剩余预约次数")
    public ResponseResult countReservationsForEachTime(Long time) {

        LocalDateTime handleTime = LocalDateTimeUtil.of(time);
        List<TimeCountVo> timeList = customerReservationService.countReservationsForEachTime(handleTime);
        return success(timeList);
    }


    /**
     * 3 新增预约
     */
    @PostMapping
    @ApiOperation(value = "新增预约")
    public ResponseResult add(@RequestBody ReservationDto reservationDto) {

        customerReservationService.add(reservationDto);
        return success();
    }

    /**
     * 4 分页查询预约
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询预约")

    public ResponseResult<PageResponse<ReservationVo>> selectByPage( @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                                     @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                                     @RequestParam(required = false) Integer status){

        PageResponse<ReservationVo> pageInfo = customerReservationService.selectByPage(pageNum, pageSize, status);
        return success(pageInfo);
    }


    /**
     * 5 取消预约
     */
    @PutMapping("/{id}/cancel")
    @ApiOperation(value = "取消预约")
    public ResponseResult cancelReservation(@PathVariable Long id){
        customerReservationService.cancelReservation(id);
        return success();
    }

}
